﻿/* 
   Copyright 2011 LUCA Studios LLC & John Evans

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

namespace LUCAStudios.Silverlight.Controls.Common
{
    /// <summary>
    /// This class supports retention of a control's previous container information and is 
    /// used by the framework during restoral operations.
    /// </summary>
    public sealed class PreviousContainerContextInfo
    {
        /// <summary>
        /// Default Constructor
        /// </summary>
        public PreviousContainerContextInfo(){}

        /// <summary>
        /// Constructor which builds the class using the given ControlBase control
        /// </summary>
        /// <param name="control"></param>
        public PreviousContainerContextInfo(ControlBase control)
        {
            if (control.ContainerContext == null) throw new LUCAControlException("Expected ContainerContext to be non-null.");

            if (!control.ContainerContext.Children.Contains(control))
                throw new LUCAControlException("Expected parameter 'control' to be a child of it's ContainerContext.");

            if (control.PreviousContainerContext != default(PreviousContainerContextInfo))
                control.PreviousContainerContext = null;

            ContainerContext = control.ContainerContext;

            Index = control.ContainerContext.Children.IndexOf(control);

            if (Index == control.ContainerContext.Children.Count - 1)
                WasLastItemInContainer = true;
        }

        /// <summary>
        /// Holds the previous index location of the control in the ContainerContext
        /// </summary>
        public int Index { get; set; }

        /// <summary>
        /// Flags if the control was the last item in the container at the time of it's removal
        /// </summary>
        public bool WasLastItemInContainer { get; set; }

        /// <summary>
        /// References the previous ContainerContext that the control was removed from
        /// </summary>
        public ContainerBase ContainerContext { get; set; }

    }
}
